<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      position: relative;
      width: 100vw;
      height: 100vh;
    }

    #modle {
      position: fixed;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      height: 200px;
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <button id="open">打开弹框</button>
  <button id="close">关闭弹框</button>
  <script>
    /**
     * 实现一个全局的模态框
     */
    const Modal = (function() {
      let instance = null;
      return function() {
        if (!instance) {
          instance = document.createElement('div');
          instance.innerHTML = '我是一个弹出框';
          instance.id = 'modle';
          instance.style.display = 'none';
          document.body.append(instance)
        }
        return instance;
      }
    })();

    document.getElementById('open').addEventListener('click', function() {
      const modal = Modal();
      modal.style.display = 'block'
    })

    document.getElementById('close').addEventListener('click', function() {
      const modal = Modal();
      modal.style.display = 'none'
    })
  </script>
</body>

</html>